require "import"
import "android.widget.Button"
import "android.widget.LinearLayout"
import "com.androlua.LuaDialog"
import "android.content.Intent"
import "android.R"
import "android.view.View"
import "android.widget.TextView"
import "android.graphics.Bitmap"
import "android.net.Uri"
import "java.io.File"
import "java.io.FileOutputStream"
import "java.util.Locale"

local currentLang = Locale.getDefault().getLanguage()

local strings = {
  ar = {
    title = "الوصف المتقدم Advanced Description",
    focusMode = "تم تفعيل وضع التركيز",
    fullScreenMode = "تم التحويل إلى وضع ملء الشاشة",
    joinGroup = "الانضمام الى الجروب الرسمي",
    close = "إغلاق",
    developer = "سايح فتحي"
  },
  fr = {
    title = "Description Avancée",
    focusMode = "Mode focus activé",
    fullScreenMode = "Mode plein écran activé",
    joinGroup = "Rejoindre le groupe officiel",
    close = "Fermer",
    developer = "Sayah Fathi"
  },
  en = {
    title = "Advanced Description",
    focusMode = "Focus mode activated",
    fullScreenMode = "Full screen mode activated",
    joinGroup = "Join Official Group",
    close = "Close",
    developer = "Sayah Fathi"
  }
}

local lang = strings[currentLang] or strings.en
local isFullScreenMode = false

local function processAndShare(app_pkg, click_actions)
  dlg.dismiss()
  task(200, function()
    service.playSoundTick()
    local store_url = "market://details?id="
    
    if this.getPackageManager().getLaunchIntentForPackage(app_pkg) then
      local cachePath = this.getExternalCacheDir().getPath()
      local screenshotFile = File(cachePath, "master_kaushik.jpg")
      
      local captureCallback = {
        onScreenCaptureDone = function(bitmap)
          if bitmap then
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, FileOutputStream(screenshotFile))
            local fileUri = this.getUriForFile(screenshotFile)
            local intent = Intent(Intent.ACTION_SEND)
              .setType("image/*")
              .setPackage(app_pkg)
              .putExtra(Intent.EXTRA_STREAM, fileUri)
            this.startActivity(intent)
            return true
          end
          return false
        end
      }

      if isFullScreenMode then
        this.getScreenShot2(captureCallback)
      else
        local node = service.getFocusView()
        if node then
          this.getScreenShot(node, captureCallback)
        else
          this.getScreenShot2(captureCallback)
        end
      end
      
    else
      this.startActivity(Intent(Intent.ACTION_VIEW).setData(Uri.parse(store_url .. app_pkg)))
    end
    
    if click_actions then
      task(1000, function()
        service.click(click_actions)
      end)
    end
  end)
end

layout={
  LinearLayout,
  layout_width="match_parent",
  layout_height="match_parent",
  orientation=1,
  background="#ff212121",

  {
    TextView,
    id = "textview",
    text = lang.title,
    layout_width = "match_parent",
    layout_height = "wrap_content",
    background="#ff000000",
    textColor="#f0f8ff",
    textSize = "22sp",
    gravity = "center",
    padding="16dp",
  },

  {
    LinearLayout,
    orientation=0,
    layout_width="match_parent",
    {
      Button,
      text="GEMINI",
      textSize="14sp",
      background="#ff3f51b5",
      textColor="#ffffffff",
      layout_width="0dp",
      layout_weight=1,
      layout_margin="4dp",
      onClick=function(v)
        processAndShare("com.google.android.apps.bard", nil)
      end,
    },
    {
      Button,
      text="BE MY EYES",
      textSize="14sp",
      background="#005aee",
      textColor="#ffffffff",
      layout_width="0dp",
      layout_weight=1,
      layout_margin="4dp",
      onClick=function(v)
        processAndShare("com.bemyeyes.bemyeyes", {{"<#loadingIndicator>20", "<#addPictureButton>60$600","%上个焦点"}})
      end,
    },
  },

  {
    LinearLayout,
    orientation=0,
    layout_width="match_parent",
    {
      Button,
      text="LOOKOUT",
      textSize="14sp",
      background="#4859b8",
      textColor="#ffffffff",
      layout_width="0dp",
      layout_weight=1,
      layout_margin="4dp",
      onClick=function(v)
        processAndShare("com.google.android.apps.accessibility.reveal", nil)
      end,
    },
    {
      Button,
      text="CHAT-GBT",
      textSize="14sp",
      background="#ff4caf50",
      textColor="#ffffffff",
      layout_width="0dp",
      layout_weight=1,
      layout_margin="4dp",
      onClick=function(v)
        processAndShare("com.openai.chatgpt", nil)
      end,
    },
  },

  {
    LinearLayout,
    orientation=0,
    layout_width="match_parent",
    {
      Button,
      text="PICCYBOT",
      textSize="14sp",
      background="#ff9800",
      textColor="#ffffffff",
      layout_width="0dp",
      layout_weight=1,
      layout_margin="4dp",
      onClick=function(v)
        processAndShare("com.piccybot.app", nil)
      end,
    },
    {
      Button,
      text="ENVISION AI",
      textSize="14sp",
      background="#ffffffff",
      textColor="#ff000000",
      layout_width="0dp",
      layout_weight=1,
      layout_margin="4dp",
      onClick=function(v)
        processAndShare("com.letsenvision.envisionai", {{"%朗读/ $4000", "Envision AI>25"}})
      end,
    },
  },

  {
    LinearLayout,
    orientation=0,
    layout_width="match_parent",
    gravity="center_vertical",
    {
      Button,
      id="toggleModeBtn",
      text=lang.focusMode,
      textSize="12sp",
      background="#ff79c6",
      textColor="#ffffffff",
      layout_width="0dp",
      layout_weight=1.3,
      layout_margin="4dp",
      onClick=function(v)
        isFullScreenMode = not isFullScreenMode
        if isFullScreenMode then
          v.setText(lang.fullScreenMode)
          v.setBackgroundColor(0xff009688)
          service.speak(lang.fullScreenMode)
        else
          v.setText(lang.focusMode)
          v.setBackgroundColor(0xffff79c6)
          service.speak(lang.focusMode)
        end
      end,
    },
    {
      TextView,
      text=lang.developer,
      textSize="16sp",
      textColor="#ff85a3",
      gravity="center",
      layout_width="0dp",
      layout_weight=0.7,
      layout_margin="4dp",
    },
  },

  {
    LinearLayout,
    orientation=0,
    layout_width="match_parent",
    {
      Button,
      text=lang.joinGroup,
      textSize="12sp",
      background="#0088cc",
      textColor="#ffffffff",
      layout_width="0dp",
      layout_weight=1.5,
      layout_margin="4dp",
      onClick=function(v)
        dlg.dismiss()
        local url = "https://telegram.me/+6zjYEyrtoL00NDQ0"
        this.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
      end,
    },
    {
      Button,
      text=lang.close,
      textSize="14sp",
      background="#ffe51c23",
      textColor="#ffffffff",
      layout_width="0dp",
      layout_weight=0.7,
      layout_margin="4dp",
      onClick=function(v)
        dlg.dismiss()
      end,
    },
  },
}

dlg=LuaDialog()
dlg.setView(loadlayout(layout))
dlg.show()
